package org.koyi.fragments;

import org.koyi.birrapp.R;
import org.koyi.entities.Bares;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class DetallesFragment extends Fragment {
	
	private Bares selectedBar = null;
	private ImageView galeriaImagenes;
	private int[] imageArray = {R.drawable.cerveceria1, R.drawable.cerveceria2, R.drawable.cerveceria3, R.drawable.cerveceria4};
	
	private static Runnable runnable = null;
	
	private static Handler handler = null;
	
	public static DetallesFragment cargaSelectedBar(Bares selectedBar)
	{		
		DetallesFragment detallesFragment = new DetallesFragment();
		
		Bundle bundle = new Bundle();
	    bundle.putSerializable("selectedBar", selectedBar);
	    detallesFragment.setArguments(bundle);
		
		return detallesFragment;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		
		getActivity().setTitle("Detalle Local");
		
		Bundle bundle = getArguments();
		
		if(bundle != null)
	    {
			selectedBar = (Bares) bundle.getSerializable("selectedBar");
			
			galeriaImagenes = (ImageView) getActivity().findViewById(R.id.galeriaImagenes); 
			int fotoId = getActivity().getResources().getIdentifier(selectedBar.getFoto() , "drawable", getActivity().getPackageName());
			galeriaImagenes.setImageResource(fotoId);
			
			handler = new Handler();
	        runnable = new Runnable() {
	            int i=0;
	            public void run() {
	            	if(getActivity() != null)
	    			{
		            	Animation animationFadeOut = AnimationUtils.loadAnimation(getActivity(), R.drawable.fade_out);
		    			galeriaImagenes.startAnimation(animationFadeOut);
		    			
		                galeriaImagenes.setImageResource(imageArray[i]);
		                
		                Animation animationFadeIn = AnimationUtils.loadAnimation(getActivity(), R.drawable.fade_in);
		    			galeriaImagenes.startAnimation(animationFadeIn);
		                
		                i++;
		                
		                if(i > imageArray.length-1)
		                {
		                	i=0;    
		                }
		                handler.postDelayed(this, 6000);
	    			}
	            }
	        };
	        handler.postDelayed(runnable, 2000);
			
	        selectedBar = new Bares();
	        
	    	selectedBar = (Bares) bundle.getSerializable("selectedBar");
	    }
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
	{             
		
	    View rootView = inflater.inflate(R.layout.detalles_fragment, container, false);
	    
	    return rootView;
	}
}
